<?php  
	/** 
	 * 入口Controller 
	 * @author wxy last update on 2016-1-20
	 * 
	 */  
	class loginController extends adminController {

		//Action白名单,用于可以再外部用url访问，形式为参数a的值
		public $initphp_list = array("login",'logindo','outLogin','ajaxLogin'); 

		//在其他方法调用前调用，一般用作权限操作等 重写before方法
		function before(){
			
		}

		// 在其他的方法调用后调用，一般可做日志操作
		function after(){
			// echo "明天：日志设置";
		}
		
	    /** 
	     * 调用TestService 
	     * @return TestService 
	     */  
	    public function getAdminService() {  
	        return InitPHP::getService("admin");  
	    }

	    /**
	     * 自定义 登录页面
	     * by wxy
	     */
	    function login(){
	    	//获取GET|POST常用的参数 (例：参数c)
	    	$n = $this->controller->get_gp("n");					//调用controller api
	    	$n = base64_decode($n);
	    	$e = $this->controller->get_gp("e");					//调用controller api

	    	//错误输出
	    	$showError = array('name' => $n);

	    	$refresh=$_COOKIE['refresh'];
			setcookie("refresh",'');

			if($_GET){
				setcookie("refresh",'1');
		    	switch ($e) {
		    		case '1':
		    			$showError['msg'] = '用户名不可为空';
		    			break;
		    		case '2':
		    			$showError['msg'] = '密码不可为空';
		    			break;
		    		case '3':
		    			$showError['msg'] = '密码或用户名错误';
		    			break;
		    		default:
		    			$showError['msg'] = '';
		    			break;
		    	}

		    	if($refresh == '1') {
		    		$showError = array();
		    	}
		    }
    	
	    	//返回JSON格式，一般做API使用  
    		//return $this->controller->ajax_return(200, "SUCCESS", array("uid" => 10), 'json');
    	
	    	$admin_id = $_COOKIE['admin_id'];
	    	if ($admin_id){
	    		//获取api跳转页面
	    		$this->controller->redirect($this->getUrl('index', 'index'), 0);		//10秒后跳转
	    	}

	    	$this->view->assign('showError', $showError);

	    	$this->view->assign('logindo', $this->getUrl('login', 'logindo'));
	 
	    	//魔板显示 view api
	    	$this->view->display("login"); //调用hello.htm模板，后缀名.htm不需要填写  
	    }

	    /**
	     * 登录验证
	     * 直接接收form提交的参数
	     */
	    function logindo(){
	    	$post = $_POST;
	    	$username = $post['username'];
	    	$password = md5($post['password']);

	    	if($username != ''){
	    		if($password != ''){
	    			$admin = $this->getAdminService()->getAdminOne(array('name'=>$username,'pwd'=>$password));
			    	if($admin['id'] > 0) {
			    		setcookie("admin_id",$admin['id']);
			    		//获取api跳转页面
			    		$this->controller->redirect($this->getUrl('index', 'index'), 1);		//10秒后跳转
			    	}else{
			    		//获取api跳转页面
			    		$this->controller->redirect($this->getUrl('login', 'login',array('n'=>base64_encode ($username),'e'=>'3')), 0);		//10秒后跳转
			    	}
	    		}else{
	    			//获取api跳转页面
			    	$this->controller->redirect($this->getUrl('login', 'login',array('n'=>base64_encode ($username),'e'=>'2')), 0);		//10秒后跳转
	    		}
	    	}else{
	    		//获取api跳转页面
			    $this->controller->redirect($this->getUrl('login', 'login',array('n'=>'','e'=>'1')), 0);		//10秒后跳转
	    	}
	    	setcookie("refresh",'');
	    }

	    /**
	     * ajax登录
	     * @return 
	     */
	    function ajaxLogin(){
	    	$username = $this->controller->get_gp("username");					
	    	$password = $this->controller->get_gp("password");
	    	$password = md5($password);

    		$admin = $this->getAdminService()->getAdminOne(array('name'=>$username,'pwd'=>$password));
	    	if($admin['id'] > 0) {
	    		setcookie("admin_id",$admin['id']);
	    		$status = 200;
	    		//获取api跳转页面
	    		$this->controller->redirect($this->getUrl('index', 'index'), 1);		//10秒后跳转
	    	}else{
	    		$status = 300;
	    		$msg = '密码或用户名错误';
	    	}
	    	//返回JSON格式，一般做API使用  
    		die($this->controller->ajax_return($status, $msg, array(), 'json'));
	    } 

	    /**
	     * 退出登录
	     * @return [type] [description]
	     */
	    function outLogin(){
	    	setcookie("admin_id",'');
	    	//获取api跳转页面
	    	$this->controller->redirect($this->getUrl('login', 'login'), 1);		//10秒后跳转
	    }  

	}  
?>